package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type SafetyEducationHandler struct {
	svc *service.SafetyEducationService
}

func NewSafetyEducationHandler(svc *service.SafetyEducationService) *SafetyEducationHandler {
	return &SafetyEducationHandler{
		svc: svc,
	}
}

func (a *SafetyEducationHandler) RegistryRoutes(server *gin.Engine) {
	ag := server.Group("/safety-education")
	ag.POST("/query", a.Query)
}

func (a *SafetyEducationHandler) Query(ctx *gin.Context) {
	type QueryReq struct {
		Category   string `json:"category"`
		ModuleName string `json:"module_name"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	SafetyEducation, err := a.svc.Query(ctx, domain.SafetyEducation{
		Category:   req.Category,
		ModuleName: req.ModuleName,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, SafetyEducation)
}
